package controllers;

import models.ru.paralainer.chat.server.annotations.HttpBasic;
import models.ru.paralainer.chat.server.dao.UserDAO;
import models.ru.paralainer.chat.server.domain.User;
import models.ru.paralainer.chat.server.dto.BaseResponseDTO;
import models.ru.paralainer.chat.server.dto.ErrorDTO;
import models.ru.paralainer.chat.server.dto.OnlineUserListDTO;
import models.ru.paralainer.chat.server.enumeration.Status;
import models.ru.paralainer.chat.server.util.OnlineUserHolder;
import org.apache.commons.lang.StringUtils;

/**
 * Created By: Sergey Talov (serg.talov@gmail.com)
 * Date: 08.08.12
 * Time: 20:29
 */
public class UserController extends BaseController {

    public static void authorise() {
        if (StringUtils.isBlank(request.user) || StringUtils.isBlank(request.password)) {
            renderJSON(new ErrorDTO(INVALID_USERNAME));
        }
        User user = UserDAO.INSTANCE.findByUsername(request.user);
        if (user == null || !request.password.equals(user.getPassword())) {
            renderJSON(new ErrorDTO(INVALID_USERNAME));
        }
        renderJSON(new BaseResponseDTO(Status.OK));
    }

    @HttpBasic
    public static void online() {
        OnlineUserHolder.INSTANCE.addUser(fetchUser());
        renderJSON(new OnlineUserListDTO(OnlineUserHolder.INSTANCE.getUserList()));
    }
}
